﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
//using Microsoft.Office.Core;
using MSExcel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Reflection;

namespace JLM2
{
    public partial class ReportList : Form
    {
        void ListReportFiles()
        {
            int i = 0;
            try
            {
                listViewReports.Items.Clear();
                string path = Application.StartupPath + "\\reports\\" ;
                DirectoryInfo TheFolder = new DirectoryInfo(path);
                if (TheFolder.GetFiles() == null)
                    return;
                if (TheFolder.GetFiles().Length == 0)
                    return;
                foreach (FileInfo NextFile in TheFolder.GetFiles().Reverse())
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = 0;     //通过与imageList绑定，显示imageList中第i项图标
                    lvi.Text = NextFile.Name;
                    lvi.SubItems.Add(NextFile.CreationTime.ToLocalTime().ToString());
                    listViewReports.Items.Add(lvi);
                    i += 1;
                }
            }
            catch (SystemException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        public ReportList()
        {
            InitializeComponent();
            ListReportFiles();
        }

        private void PrintReport(string reportFile)
        {
            try
            {
                string dir = Application.StartupPath;
                MSExcel.Application app = new MSExcel.Application();
                MSExcel.Workbooks wbks = app.Workbooks;
                MSExcel._Workbook _wbk = wbks.Add(reportFile);
                MSExcel.Sheets shs = _wbk.Sheets;

                //i是要取得的sheet的index
                MSExcel._Worksheet _wsh = (MSExcel._Worksheet)shs.get_Item(1);
                _wbk.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                app.Quit();
                Marshal.ReleaseComObject(app);
                app = null;
            }
            catch (SystemException se)
            {
                 MessageBox.Show("打印失败！");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string day = dateTimePicker1.Value.ToShortDateString();
            string path = Application.StartupPath + "\\reports\\";

            int i = 0;
            try
            {
                listViewReports.Items.Clear();
                DirectoryInfo TheFolder = new DirectoryInfo(path);
                if (TheFolder.GetFiles() == null)
                    return;
                if (TheFolder.GetFiles().Length == 0)
                    return;
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = 0;     //通过与imageList绑定，显示imageList中第i项图标
                    lvi.Text = NextFile.Name;
                    lvi.SubItems.Add(NextFile.CreationTime.ToLocalTime().ToString());
                    listViewReports.Items.Add(lvi);
                    i += 1;
                }
            }catch(SystemException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem item in listViewReports.SelectedItems)
                {
                    //MessageBox.Show(item.SubItems[0].Text);
                    string str = Application.StartupPath + "\\reports\\" + item.SubItems[0].Text;
                    File.Delete(str);
                    MessageBox.Show("文件已删除！");
                }
            }catch(SystemException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        private void listViewReports_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string day = dateTimePicker1.Value.ToShortDateString();
                string path = Application.StartupPath + "\\reports\\" + listViewReports.SelectedItems[0].Text;
                System.Diagnostics.Process.Start(path);
            }
            catch (SystemException se)
            {

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewReports.SelectedItems)
            {
                try
                {
                    string day = dateTimePicker1.Value.ToShortDateString();
                    string path = Application.StartupPath + "\\reports\\" + listViewReports.SelectedItems[0].Text;
                    PrintReport(path);
                }
                catch (SystemException se)
                {

                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                string day = dateTimePicker1.Value.ToShortDateString();
                string path = Application.StartupPath + "\\reports\\" + listViewReports.SelectedItems[0].Text;
                System.Diagnostics.Process.Start(path);
            }catch(SystemException se)
            {

            }
        }
    }
}
